import { DeviceTypeEnum } from "@/common/enums/menu.enum"
/**
 * 设备类型检测工具
 * 用于判断当前设备是移动设备还是PC设备
 */

/**
 * 检测是否为移动设备
 * @param {string} userAgent - 用户代理字符串
 * @returns {boolean} 如果是移动设备返回true，否则返回false
 */
export const isMobile = (userAgent: string): boolean => {
  if (!userAgent) return false;
  const mobileRegex = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|Phone/i;
  return mobileRegex.test(userAgent);
}

/**
 * 检测是否为平板设备
 * @param {string} userAgent - 用户代理字符串
 * @returns {boolean} 如果是平板设备返回true，否则返回false
 */
export const isTablet = (userAgent: string): boolean => {
  if (!userAgent) return false;
  const tabletRegex = /iPad|Android(?!.*Mobile)|Tablet|PlayBook|RIM Tablet OS/i;
  return tabletRegex.test(userAgent);
}

/**
 * 获取设备类型
 * @param {string} userAgent - 用户代理字符串
 * @returns {DeviceTypeEnum} 返回设备类型枚举值
 */
export const getDeviceType = (userAgent: string): DeviceTypeEnum => {
  if (isMobile(userAgent) && !isTablet(userAgent)) {
    return DeviceTypeEnum.MOBILE;
  } else if (isTablet(userAgent)) {
    return DeviceTypeEnum.TABLET;
  }
  return DeviceTypeEnum.DESKTOP;
}
